# 冒泡排序
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

# 选择排序
def selection_sort(arr):
    n = len(arr)
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
    return arr

# 插入排序
def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
    return arr

# 张茜璐——快速排序
def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]  # 选择中间元素为枢轴
    left = [x for x in arr if x < pivot]  # 比枢轴小的元素
    middle = [x for x in arr if x == pivot]  # 与枢轴相等的元素
    right = [x for x in arr if x > pivot]  # 比枢轴大的元素
    return quick_sort(left) + middle + quick_sort(right)


# 李慧——归并排序
def merge_sort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    return merge(left, right)


def merge(left, right):
    merged = []
    i = j = 0
    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            merged.append(left[i])
            i += 1
        else:
            merged.append(right[j])
            j += 1
    merged.extend(left[i:])
    merged.extend(right[j:])
    return merged


# 杨雨菲——计数排序作为基数排序的辅助函数
def counting_sort_for_radix(input_array, exp):
    n = len(input_array)
    output = [0] * n  # 输出数组
    count = [0] * 10  # 计数数组

    # 存储每个数字出现次数
    for i in range(n):
        index = input_array[i] // exp
        count[index % 10] += 1

    # 更改count[i]，以便count[i]现在包含实际位置
    for i in range(1, 10):
        count[i] += count[i - 1]

    # 根据当前位构建输出数组
    i = n - 1
    while i >= 0:
        index = input_array[i] // exp
        output[count[index % 10] - 1] = input_array[i]
        count[index % 10] -= 1
        i -= 1

    # 复制output数组到input_array，所以input_array现在包含排序后的数字
    for i in range(n):
        input_array[i] = output[i]

# 杨雨菲——基数排序
def radix_sort(arr):
    max1 = max(arr)

    # 做counting_sort_for_radix根据每个位。我们先从个位开始，然后是十位、百位等
    exp = 1
    while max1 // exp > 0:
        counting_sort_for_radix(arr, exp)
        exp *= 10
    return arr

# 蒋凤吉——堆排序
def heap_sort(arr):
    arr = arr.copy()
    n = len(arr)

    # 构建最大堆
    for i in range(n // 2 - 1, -1, -1):
        _heapify(arr, n, i)

    # 逐个提取元素
    for i in range(n - 1, 0, -1):
        # 交换堆顶元素和当前未排序部分的最后一个元素
        arr[0], arr[i] = arr[i], arr[0]
        # 重新调整堆
        _heapify(arr, i, 0)
    return arr


def _heapify(arr, n, i):
    largest = i
    left = 2 * i + 1
    right = 2 * i + 2

    if left < n and arr[left] > arr[largest]:
        largest = left

    if right < n and arr[right] > arr[largest]:
        largest = right

    if largest != i:
        arr[i], arr[largest] = arr[largest], arr[i]
        # 递归调整受影响的子树
        _heapify(arr, n, largest)

# 主函数，用于调用排序算法
if __name__ == "__main__":
    test_array = [64, 34, 25, 12, 22, 11, 90]
    print("原始数组:", test_array)
    print("冒泡排序结果:", bubble_sort(test_array.copy()))
    print("选择排序结果:", selection_sort(test_array.copy()))
    print("插入排序结果:", insertion_sort(test_array.copy()))
    print("快速排序结果:", quick_sort(test_array.copy()))
    print("归并排序结果:", merge_sort(test_array.copy()))
    print("基数排序结果:", radix_sort(test_array.copy()))
    print("堆排序结果:", heap_sort(test_array.copy()))